package com.srd.phone.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.res.AssetManager;
import android.view.WindowManager;

import com.srd.phone.bean.BarrioInfo;
import com.srd.phone.utils.CommonUtils;
import com.srd.phone.utils.Constants;
import com.srd.phone.utils.PreferenceUtils;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BarrioDialog extends Dialog {

    private Context mContext;

    public BarrioDialog(Context context) {
        super(context);
        this.mContext = context;
    }

    public BarrioDialog(Context context, int theme) {
        super(context, theme);
        this.mContext = context;
    }

    protected BarrioDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.mContext = context;
    }

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
        WindowManager.LayoutParams lp = this.getWindow().getAttributes();
        lp.width = PreferenceUtils.getInstance().getData(Constants.APP_WIDTH, 0);
        this.getWindow().setAttributes(lp);
    }

    @Override
    public void dismiss() {
        super.dismiss();
    }

    /**
     * 所有省
     */
    public static BarrioInfo[] mProvinceDatas;
    /**
     * 省下面的市
     */
    public static Map<Integer, BarrioInfo[]> mCitisDatasMap = new HashMap<>();
    /**
     * 市下面的区
     */
    public static Map<Integer, BarrioInfo[]> mDistrictDatasMap = new HashMap<>();

    /**
     * 当前省的名称
     */
    protected BarrioInfo mCurrentProvice;
    /**
     * 当前市的名称
     */
    protected BarrioInfo mCurrentCity;
    /**
     * 当前区的名称
     */
    protected BarrioInfo mCurrentDistrict;

    /**
     * 解析省市区的json数据
     */
    protected void initDatas() {
        if (mProvinceDatas != null && mProvinceDatas.length != 0 && mCitisDatasMap.size() != 0 && mDistrictDatasMap.size() != 0) {
            return;
        }
        AssetManager asset = mContext.getAssets();
        try {
            InputStream input = asset.open("province_data.json");
            String value = CommonUtils.retrieveContent(input);
            List<BarrioInfo> list = BarrioInfo.parser(value);
            mProvinceDatas = new BarrioInfo[list.size()];
            for (int i = 0; i < list.size(); i++) {
                BarrioInfo province = list.get(i);
                mProvinceDatas[i] = province;
                BarrioInfo[] citys = new BarrioInfo[province.list.size()];
                for (int j = 0; j < province.list.size(); j++) {
                    BarrioInfo city = province.list.get(j);
                    citys[j] = city;
                    BarrioInfo[] districts = new BarrioInfo[city.list.size()];
                    for (int k = 0; k < city.list.size(); k++) {
                        BarrioInfo district = city.list.get(k);
                        districts[k] = district;
                    }
                    mDistrictDatasMap.put(city.id, districts);
                }
                mCitisDatasMap.put(province.id, citys);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
